const ctx = require.context("./", true, /\.vue$/);

function rename(filename) {
  return filename
    .replace(/\.+\//, "")
    .replace(/\.vue/, "")
    .replace(/\//g, "-")
    .replace(/^(\w)/, v => v.toLowerCase());
}

const components = ctx.keys().map(filename => ({
  name: rename(filename),
  component: ctx(filename).default
}));

const Plugin = Vue => {
  if (Plugin.installed) return;
  Plugin.installed = true;
  components.forEach(el => {
    Vue.component(el.name, el.component);
  });
};

export default Plugin;
